package com.twb.system.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotBlank;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;
import com.twb.system.common.SystemGlobal;
import com.twb.system.entity.common.dto.GetCacheDataDto;
import com.twb.system.entity.twbDataDic.TwbDataDic;
import com.twb.system.entity.twbDataDic.dto.TwbDataDicDto;
import com.twb.system.service.ITwbDataDicService;
import com.twb.system.service.ITwbSysconfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Title TwcommonController.java
 * @Description
 * 
 * @author DSY
 * @date 2016年12月19日 下午2:07:40
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/common")
@Api(value = "TwbCommonController", tags = "系统通用接口")
public class TwbCommonController extends BaseController {

	/*@Autowired
	ITwbProjectService twbProjectService;*/
	
	@Autowired
	ITwbSysconfigService twbSysconfigService;

	@Autowired
	ITwbDataDicService twbDataDicService; 
	
	/**
	 * @Title  getCacheData
	 * @Class: GsCommonController.java
	 * @Package: com.goldsaas.controller
	 * @Description 获取缓存数据
	 * @return
	 * @throws Exception
	 * @author DSY
	 * @date 2018年9月11日 上午11:03:36
	 */
	@SuppressWarnings("unchecked")
	@Auth(verifyURL = false)
	@RequestMapping(value = "/getCacheData", method = RequestMethod.POST)
	@ApiOperation(value = "获取缓存数据，包括字典数据、系统配置、登录用戶信息")
	public Result<GetCacheDataDto> getCacheData() throws Exception {
		GetCacheDataDto dto = new GetCacheDataDto();
		dto.setDataDic(SystemGlobal.dataDic());
		dto.setSysConfig(SystemGlobal.sysConfig());
		dto.setUserConfig(SystemGlobal.getLoginUserConfig());
		return Results.successWithData(dto);
	}

	/**
	 * @Title refreshCacheData
	 * @Description 刷新缓存数据
	 * @return
	 * @throws Exception
	 * @author DSY
	 * @date 2020年5月20日 下午2:27:41
	 */
	@Auth(verifyURL = false)
	@RequestMapping(value = "/refreshCacheData", method = RequestMethod.POST)
	@ApiOperation(value = "刷新缓存")
	public Result<GetCacheDataDto> refreshCacheData() throws Exception {
		
		try {

			// 数据字典
			twbDataDicService.initRedisDataDic();
			
			// 系统配置
			twbSysconfigService.initRedisSysConfig();

			
			GetCacheDataDto dto = new GetCacheDataDto();
			dto.setDataDic(SystemGlobal.dataDic());
			dto.setSysConfig(SystemGlobal.sysConfig());
		
			return Results.successWithData(dto);
		} catch (Exception e) {
			e.printStackTrace();
			
			return Results.failure("初始化缓存失败!");
		}
	}

	/**
	 * 字符串base64加密
	 * 
	 * @param text
	 * @return
	 */
	public String base64Encode(String text) {

		String resultStr = "";

		try {
			Base64 base64 = new Base64();
			byte[] dataBytes = text.getBytes("UTF-8");

			resultStr = new String(base64.encode(dataBytes));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return resultStr;
	}

	
	/**
	 * @Title listDataDic
	 * @Description 获取字典列表
	 * @param typeCode
	 * @return
	 * @throws Exception
	 * @AuthorOriginally DSY
	 * @date 2022年12月22日 上午10:35:00
	 */
	@Auth(verifyURL = false)
	@GetMapping("/listDataDic")
	@ApiOperation(value = "获取字典列表")
	public Result<List<TwbDataDicDto>> listDataDic(
			@NotBlank @RequestParam @ApiParam(value = "字典编码", name = "typeCode", required = true) 
			String typeCode) throws Exception {
		List<TwbDataDic> list = SystemGlobal.dataDic(typeCode);
		List<TwbDataDicDto> dtos = BeanUtils.copyList(TwbDataDicDto.class, list);
		
		return Results.successWithData(dtos);
	}
	
	/**
	 * @Title getDataDicName
	 * @Description 获取字典名称
	 * @param typeCode
	 * @param dataValue
	 * @return
	 * @throws Exception
	 * @AuthorOriginally DSY
	 * @date 2022年12月22日 上午10:35:07
	 */
	@Auth(verifyURL = false)
	@GetMapping("/getDataDicName")
	@ApiOperation(value = "获取字典名称")
	public Result<String> getDataDicName(
			@NotBlank @RequestParam @ApiParam(value = "字典编码", name = "typeCode", required = true) 
			String typeCode,
			@NotBlank @RequestParam @ApiParam(value = "字典值", name = "dataValue", required = true) 
			String dataValue) throws Exception {
		String name = SystemGlobal.dataName(typeCode, dataValue);
		
		return Results.successWithData(name);
	}
	
	
	/**
	 * @Title listSystemConfig
	 * @Description 获取系统配置列表
	 * @return
	 * @throws Exception
	 * @AuthorOriginally DSY
	 * @date 2022年12月22日 上午10:38:13
	 */
	@Auth(verifyURL = false)
	@GetMapping("/listSystemConfig")
	@ApiOperation(value = "获取系统配置列表")
	public Result<Map<String, String>> listSystemConfig() throws Exception {
		Map<String, String> list = SystemGlobal.sysConfig();
		
		return Results.successWithData(list);
	}
	
	
	/**
	 * @Title getSystemConfig
	 * @Description 获取系统配置
	 * @param sysName
	 * @return
	 * @throws Exception
	 * @AuthorOriginally DSY
	 * @date 2022年12月22日 上午10:38:20
	 */
	@Auth(verifyURL = false)
	@GetMapping("/getSystemConfig")
	@ApiOperation(value = "获取系统配置")
	public Result<String> getSystemConfig(
			@NotBlank @RequestParam @ApiParam(value = "配置编码", name = "sysName", required = true) 
			String sysName) throws Exception {
		String rule = SystemGlobal.sysConfig(sysName);
		return Results.successWithData(rule);
	}
}
